<?php

class Core_Agent_Component_Subject_Controller_Article extends Dcmp_Component_Controller {

    public function createAction(){
        $category = App::getModel('article/collection_category')->load();
        Kd_Web::getPage()->getNavigation()
                ->addLink($this->getSession()->get('current_subject_name'),$this->getUrl(NULL,'index','subject'))
                ->addLabel('创建文章');
        return $this->viewResult(array('category'=>$category));
    }

    public function editAction($id){
        $model = App::getModel('article/collection_content')->joinSubject()->filterBy('id',$id)->load($id);
        $subject = App::getModel('subject/content')->load($model[0]['subject_id']);
        $category = App::getModel('article/collection_category')->load();
        Kd_Web::getPage()->getNavigation()
                ->addLink($this->getSession()->get('current_subject_name'),$this->getUrl(NULL,'index','subject'))
                ->addLink('文章列表',$this->getUrl($this->getSession()->get('current_subject_id'),'index','article'))
                ->addLabel($model[0]->getTitle());
        return $this->viewResult(array('model'=>$model->getFirstItem(),'category'=>$category),'create');
    }

    public function savePostAction($state) {
        if ($this->saveModel($state, $model)) {
            return $this->redirectResult($this->getUrl(array('subject_id'=>$this->getSession()->get('current_subject_id')),'index'));
        }
        return $this->createAction()->setModel($model);
    }

    private function saveModel($state, &$savedModel = NULL) {
        $pictureName = array();
        $data = $this->getRequest()->getForm('content');
        $subject = App::getModel('subject/content')->load($this->getSession()->get('current_subject_id'));
        $model = App::getModel('article/content');
        if (isset($data['id']) && !empty($data['id'])) {
            $model->load($data['id']);
            $subject->updateArticleRelation($data,'pending');
        }
        $model  ->addData($data)
                ->setStatus($state)
                ->setOwnerId($this->getIdentity()->getId());
        $savedModel = $model;
        foreach($data['picture'] as $picture){
            $pictureName[] = basename($picture);
        }
        $model->setPicture(implode(',', $pictureName));
        if (($errors = $model->validate() === TRUE)) {
            $model->save();
            if (empty($data['id'])) {
                $subject->associateArticle($model,'pending');
            }
            
            Kd_Util_IO::moveFiles(Kd_Web_Util::urlsToPaths($data['picture']),
            					  DP_PUB.'article'.DS.'content'.DS.$model->getId(),True);
            $this->getMessager()->addInform($this->__('share.message.success'));
            return TRUE;
        }
        else {
            $this->getMessager()->addError($errors);
            return FALSE;
        }
    }

    public function indexAction($subject_id,$page=1,$sort = NULL){
        $subject = App::getModel('subject/content')->load($subject_id);
        $articles = $subject->getArticles()
                            ->joinCategory()
                            ->filterBy('ownerid',$this->getIdentity()->getId());
        if(!is_null($sort)){
            $articles->sortBy($sort,'desc');
        }
        $articles->load();
        $records = $articles->loadCount();
        $args = array_merge($this->getRequest()->getAllParameters(), array('page' => '{index}'));
        $pagination = new Kd_Html_Pagination();
        $pagination->setPageIndex($page)
                   ->setPageSize(10)
                   ->setPageRange(6)
                   ->setRecordCount($records)
                   ->setUrlFormat($this->getUrl($args));
        Kd_Web::getPage()->setTitle('文章列表')->setKeyword('');
        Kd_Web::getPage()->getNavigation()
                ->addLink($subject->getName(),$this->getUrl(NULL,'index','subject'))
                ->addLabel('文章列表');
        $this->getSession()->set('current_subject_id',$subject_id);
        $this->getSession()->set('current_subject_name',$subject->getName());
        $this->getVariables()
                ->setData('article_list', $articles)
                ->setData('article_list_count', $records)
                ->setData('article_list_page_index', $page)
                ->setData('article_list_page_size', 20)
                ->setData('article_list_pagination', $pagination);
        return $this->viewResult(array('articles'=>$articles,'pagination'=>$pagination));
    }

    public function previewAction($id){
        $this->getSession()->set('is_article_preview','a');
        return $this->redirectResult('/article/content/preview/id/'.$id);
    }
}